package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.activation_rsnCode_report.ActivationRsnCodeReportDataBean;
import com.navi.bean.out.activation_rsnCode_report.ActivationRsnCodeReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.core.repository.NaviRepository;
import com.navi.mould1.service.IActivationRsnCodeReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;
import static com.navi.mould1.reportUtils.reportUtil.getCurrenDay;


@Slf4j
@Service
public class ActivationRsnCodeReportServiceImpl implements IActivationRsnCodeReportService {

    @Autowired
    private NaviRepository dao;


    public List<ActivationRsnCodeReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        sqlVo.appendSql("SELECT");
        sqlVo.appendSql("*");
        sqlVo.appendSql("FROM icim_jn.sum_tool_st_rsn_code_hour A");
        sqlVo.appendSql("WHERE 1=1");
        if (StringUtils.isNotEmpty(conditions.getFab_id())) sqlVo.appendSql("AND A.FAB_ID_FK in" + jointInSql(conditions.getFab_id()));
        if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo.appendSql("AND A.LINE_ID_FK in" + jointInSql(conditions.getBay_id()));
        if (StringUtils.isNotEmpty(conditions.getToolg_id())) sqlVo.appendSql("AND A.TOOLG_ID_FK in" + jointInSql(conditions.getToolg_id()));
        if (StringUtils.isNotEmpty(conditions.getTool_type())) sqlVo.appendSql("AND A.TOOL_ID_FK in(select tool_id from icim_jn.bis_tool where unit_typ in" + jointInSql(conditions.getTool_type()) + ")");
        if (StringUtils.isNotEmpty(conditions.getTool_id())) sqlVo.appendSql("AND A.TOOL_ID_FK in" + jointInSql(conditions.getTool_id()));
        if (StringUtils.isNotEmpty(conditions.getShift_id())) sqlVo.appendSql("AND A.SUM_SHIFT in" + jointInSql(conditions.getShift_id()));
        if (StringUtils.isNotEmpty(conditions.getTool_stat())) sqlVo.appendSql("AND A.TOOL_STAT in" + jointInSql(conditions.getTool_stat()));
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND CONCAT(SUM_DATE, ' ', SUM_HOUR, ':00:00') >= ?", conditions.getStart_time()+":00:00");
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND CONCAT(SUM_DATE, ' ', SUM_HOUR, ':00:00') < ?", conditions.getEnd_time()+":00:00");

        List<ActivationRsnCodeReportDataBean> resultDatas = mySql_QueryRunner_QueryListBySql(sqlVo, ActivationRsnCodeReportDataBean.class);

        Map<String, List<ActivationRsnCodeReportDataBean>> dataBean_Map = new HashedMap();

        if ("FAB".equals(conditions.getGroup_by())) {
            dataBean_Map = resultDatas.stream().filter(data -> {    //以厂别展示: 101以丝网为代表，102A、102B以串焊为代表
                if ("101A".equals(data.getFab_id_fk()) && "$TSWYS".equals(data.getToolg_id_fk())) return true;
                if ("102A".equals(data.getFab_id_fk()) && "102A-TCH".equals(data.getToolg_id_fk())) return true;
                if ("102B".equals(data.getFab_id_fk()) && "102B-TCH".equals(data.getToolg_id_fk())) return true;
                return false;
            }).collect(Collectors.groupingBy(data -> data.getRsn_code_dsc() + getCurrenDay(data.getSum_date(), data.getSum_hour()) + (StringUtils.isEmpty(conditions.getShift_id()) ? "" : data.getSum_shift())));
        }else if ("LINE".equals(conditions.getGroup_by())) {
            dataBean_Map = resultDatas.stream().filter(data -> {    //以线别展示: 101以丝网为代表，102A、102B以串焊为代表
                if ("101A".equals(data.getFab_id_fk()) && "$TSWYS".equals(data.getToolg_id_fk())) return true;
                if ("102A".equals(data.getFab_id_fk()) && "102A-TCH".equals(data.getToolg_id_fk())) return true;
                if ("102B".equals(data.getFab_id_fk()) && "102B-TCH".equals(data.getToolg_id_fk())) return true;
                return false;
            }).collect(Collectors.groupingBy(data -> data.getRsn_code_dsc() + getCurrenDay(data.getSum_date(), data.getSum_hour()) + data.getLine_id_fk() + (StringUtils.isEmpty(conditions.getShift_id()) ? "" : data.getSum_shift())));
        }else if ("TOOLG".equals(conditions.getGroup_by())) {
            dataBean_Map = resultDatas.stream().collect(Collectors.groupingBy(data -> data.getRsn_code_dsc() + getCurrenDay(data.getSum_date(), data.getSum_hour())  + data.getLine_id_fk() + data.getToolg_id_fk() + (StringUtils.isEmpty(conditions.getShift_id()) ? "" : data.getSum_shift())));
        }else if ("TOOL".equals(conditions.getGroup_by())) {
            dataBean_Map = resultDatas.stream().collect(Collectors.groupingBy(data -> data.getRsn_code_dsc() + getCurrenDay(data.getSum_date(), data.getSum_hour())  + data.getLine_id_fk() + data.getToolg_id_fk() + data.getTool_id_fk() + (StringUtils.isEmpty(conditions.getShift_id()) ? "" : data.getSum_shift())));
        }

        List<ActivationRsnCodeReportGridOut> gridOut_list = new ArrayList<>();

        dataBean_Map.forEach((key, list) -> {
            ActivationRsnCodeReportGridOut gridOut = new ActivationRsnCodeReportGridOut();
            gridOut.setSum_date(getCurrenDay(list.get(0).getSum_date(), list.get(0).getSum_hour()) );
            gridOut.setSum_shift(list.get(0).getSum_shift());
            gridOut.setFab_id(list.get(0).getFab_id());
            gridOut.setFab_id_fk(list.get(0).getFab_id_fk());
            gridOut.setLine_id(list.get(0).getLine_id());
            gridOut.setLine_id_fk(list.get(0).getLine_id_fk());
            gridOut.setToolg_id(list.get(0).getToolg_id());
            gridOut.setToolg_id_fk(list.get(0).getToolg_id_fk());
            gridOut.setTool_id(list.get(0).getTool_id());
            gridOut.setTool_id_fk(list.get(0).getTool_id_fk());
            gridOut.setTool_stat(list.get(0).getTool_stat());
            gridOut.setRsn_code(list.get(0).getRsn_code());
            gridOut.setRsn_code_dsc(list.get(0).getRsn_code_dsc());
            gridOut.setKeep_second(list.stream().filter(data -> data.getKeep_second() != null).mapToInt(data -> Integer.parseInt(data.getKeep_second())).sum());
            gridOut.setTotal_second(list.stream().filter(data -> data.getTotal_second() != null).mapToInt(data -> Integer.parseInt(data.getTotal_second())).sum());
            gridOut.computeMinute();
            gridOut_list.add(gridOut);
        });

        return gridOut_list;
    }





}
